package com.atguigu.gmallre.mock.service.impl;


import com.alibaba.fastjson.JSON;
import com.atguigu.gmallre.mock.bean.LogDb;
import com.atguigu.gmallre.mock.config.AppConfig;
import com.atguigu.gmallre.mock.log.AppMain;
import com.atguigu.gmallre.mock.mapper.LogDbMapper;

import com.atguigu.gmallre.mock.service.LogService;

import com.atguigu.mock.util.HttpUtil;
import com.atguigu.mock.util.KafkaUtil;
import com.atguigu.mock.util.LogUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单明细表 服务实现类
 * </p>
 *
 * @author zc
 * @since 2020-02-23
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogDbMapper, LogDb> implements LogService {

    @Override
    public void doLog(AppMain appMain) {
        if (AppConfig.mock_log_type.equals("file")) {
            LogUtil.log(appMain.toString());
        } else if (AppConfig.mock_log_type.equals("http")) {
            HttpUtil.get(appMain.toString(), AppConfig.mock_log_http_url);
        } else if (AppConfig.mock_log_type.equals("kafka")) {
            KafkaUtil.send(AppConfig.kafka_topic, appMain.toString(), AppConfig.kafka_server);
        }

        if (AppConfig.isLogDb) {
            LogDb logDb = new LogDb();
            logDb.setLog(JSON.toJSONString(appMain));
            save(logDb);
        }

        try {
            Thread.sleep(AppConfig.log_sleep);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
